<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<title>直播自动添加点击数</title>
	<link href="https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet">
	<link href="https://cdn.bootcss.com/open-iconic/1.1.1/font/css/open-iconic-bootstrap.min.css" rel="stylesheet">
	<style>
  html,body{height:100%}
  form{position: relative;}
  .reset{position:absolute;top:-50px;right:0;font-size:12px;}
  .btn{cursor:pointer;}
  </style>
</head>

<body>
	<div class="container" style="height:100%;padding-top:15px">
		<h3>直播自动添加点击数</h3>
		<hr>
		<form>
			<div class="form-group">
				<label for="group_id" class="col-form-label">group_id</label>
				<input type="text" class="form-control" id="group_id" name="group_id" ></div>
			<div class="form-row">
				<div class="form-group col-6">
					<label for="click_num" class="col-form-label">目标票数</label>
					<input type="text" class="form-control" id="click_num" name="click_num" value="1"></div>
				<div class="form-group col-6">
					<label for="click_tim var " class="col-form-label">自动增长时长(秒数)</label>
					<input type="text" class="form-control" id="click_time" name="click_time" value="3600"></div>
			</div>

			<div class="form-group row buttons">
				<div class="col-10">
					<button type="button" class="btn btn-primary start Click" disabled>开始自动点击</button>
				</div>
			</div>
			<button type="reset"  class="btn btn-default btn-sm reset">重置表单</button>
		</form>
		<div class="alert alert-danger" role="alert" id="alert-text" style="font-size: 12px;font-weight: bold">
			开始自动点击后，请勿关闭此页面！！ [当前点击数：
			<span id="clicknum"></span>
			]
		</div>
		<div class="alert alert-light border border-secondary" role="alert" id="infos" style="border-color:#ccc!important;overflow-y:auto"></div>
	</div>

	<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
	<script src="https://cdn.bootcss.com/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
	<script src="https://cdn.bootcss.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
	<script>
    var mainTimeLine;//自动点击Timeout
    var wait_time;//自动点击间隔时长
      var group_id;
      var click_num;//目标点击数
      var max_clicks;//自动点击间隔时长随机的上限
      var min_clicks;//自动点击间隔时长随机的下限
      var current_click_num=0;//当前group_id的点击数
      var get_click_num_time;//获取当前点击数的Timeout
      var is_valid_live=false;//是否是有效的group_id;
      var flush_click_num_time=5;//获取当前点击数的间隔时长

    $("#infos").height($(".container").height()-$("#infos").offset().top-50);

    $(".Click").click(function(){
      var click_time=$("#click_time").val();
      group_id=$("#group_id").val();
      click_num=$("#click_num").val();

      wait_time=click_time/((click_num-current_click_num)/40);
      max_clicks=wait_time*1.2;
      min_clicks=wait_time*0.8;

      flush_click_num_time=flush_click_num_time>=wait_time?wait_time:flush_click_num_time;

      var _this=this;
      if(group_id=="" || click_num==""){
        alert("请填写完整数据")
      }else{
        if($(_this).hasClass("start")){
    		$("form input").attr("readonly",true);
    		$(".reset").attr("disabled",true);
            makeClicks();
          $(_this).html("停止（stop）").attr("class","btn btn-secondary end Click");
        }else if($(_this).hasClass("end")){
        	$("form input").attr("readonly",false);
        	$(".reset").removeAttr("disabled");
          clearTimeout(mainTimeLine);
          flush_click_num_time=5;
          logs("00000","已经停止点击");
          $(_this).html("开始自动点击").attr("class","btn btn-primary start Click");
        }
      }
     
      
    })

    $("#group_id").change(function(){
    	group_id=$("#group_id").val();
		clearTimeout(get_click_num_time);
		if(group_id!=""){
			getClickNum();
		}else{
    		$(".Click").attr("disabled",true);
    	}
    	
    })

    function makeClicks(){
    	if(is_valid_live){
    		$.ajax({
		        cache:false,
		        type:"post",
		        async:false,
		        dataType:"jsonp",
		        data:{group_id:group_id},
		        url:"http://plive.appwuhan.com/common/wRecord2",
		        success:function(data){
		         	logs(data.errcode,data.errmsg)
		         	if(current_click_num<click_num){
						mainTimeLine=setTimeout(function(){					
							makeClicks();									
						},Math.round((Math.random()*(max_clicks-min_clicks)+min_clicks))*1000);
					}else{
						$(".end").click();
					}
		        }
		    })
    	}
      
    }
    $(".reset").click(function(){
    	group_id="";
    	$("#clicknum").html("");
    })

    function logs(code,info){
    	 var time=new Date();
          $("#infos").append("<p>"+time.toLocaleString()+" : ["+code+"] "+info+"</p>")
          $("#infos").get(0).scrollTop = $("#infos").get(0).scrollHeight;
    }

    var getClickNum=function(){
    	if(group_id!=""){
	    	$.getJSON("http://openapi.appwuhan.net/mapi/Live_details/getLiveJoinNum?groupId="+group_id,function(data){
	    		if(data.resultCode==0){
	    			$(".Click").removeAttr("disabled");
	    			is_valid_live=true;
	    			current_click_num=data.joinNum;
	    			$("#clicknum").html(current_click_num);
	    			if(current_click_num>=click_num){
	    				flush_click_num_time=5;
	    				$(".end").click();
	    			}
	    			get_click_num_time=setTimeout(function(){
	    				getClickNum();
	    			},flush_click_num_time*1000);
	    		}else{
	    			is_valid_live=false;
	    			current_click_num=0;
	    			$("#clicknum").html(data.message);
	    			$(".Click").attr("disabled",true);
	    		}
	    	})
    	}else{
    		$(".reset").removeAttr("disabled");
    		$(".Click").attr("disabled",true);
    	}
    }

    



  </script>
</body>
</html>